/**
 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const ZERO = 0;
const TWENTY_TWO_HUNDREDTH = 0.22;
const ONE = 1;
const TWO = 2;
const THREE = 3;
const PRECISION = 3;

import { Simple, SumDouble, SumBoxedDouble, SumString, ReplaceFirst } from './lib';

function AssertEq<T>(a: T, b: T) {
  console.log(`AssertEq: '${a}' === '${b}'`);
  if (a !== b) {
    throw new Error(`AssertEq failed: '${a}' === '${b}'`);
  }
}

export function main() {
  testFunctions();
}

function testFunctions() {
  AssertEq(SumDouble(THREE, TWENTY_TWO_HUNDREDTH).toFixed(PRECISION), (THREE + TWENTY_TWO_HUNDREDTH).toFixed(PRECISION));
  AssertEq(SumBoxedDouble(THREE, TWENTY_TWO_HUNDREDTH).toFixed(PRECISION), (THREE + TWENTY_TWO_HUNDREDTH).toFixed(PRECISION));
  AssertEq(SumString('Hello from ', 'Panda!!'), 'Hello from Panda!!');

  let simpleArray: Simple[] = [new Simple(ZERO), new Simple(ONE), new Simple(TWO)];
  simpleArray.forEach((e: Simple, idx: number) => { AssertEq(e.val, idx); });
  AssertEq(ReplaceFirst(simpleArray, new Simple(THREE))[0].val, THREE);
}
